package com.team.hiring.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.team.hiring.common.util.DBUtil;
import com.team.hiring.talent.entity.ResumeEntity;

public class CommonDao {

	public boolean updatePassword(String email, String newpassword,
			String accountType) {
		boolean result = false;
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		if("company".equals(accountType)){
			try{
				String sql = "UPDATE company set password=? WHERE email=?";
				prepareStatement = connection.prepareStatement(sql);
				prepareStatement.setString(1,newpassword);
				 prepareStatement.setString(2,email);
				 int row = prepareStatement.executeUpdate();
				 if (row !=0){
					 result = true;
				 }
				}catch (SQLException e) {
					e.printStackTrace();
				}finally{
					DBUtil.close(prepareStatement, connection);
				}
		}else if("talent".equals(accountType)){}
		try{
			String sql = "UPDATE talent set password=? WHERE email=?";
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1,newpassword);
			 prepareStatement.setString(2,email);
			 int row = prepareStatement.executeUpdate();
			 if (row !=0){
				 result = true;
			 }
			}catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.close(prepareStatement, connection);
			}
		
		return result;
	}
	
	
	
	
	
	/**
	 * 进入主页的职位详细前查询简历
	 * @param request
	 * @param response
	 */
	public List<ResumeEntity> selectResume(int talentId) {
		List<ResumeEntity> resumeEntityList = new ArrayList<>();
		Connection connection = DBUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		 try {
			 	String sql = "SELECT * FROM resume r WHERE r.talent_id=?";
				prepareStatement = connection.prepareStatement(sql);
				prepareStatement.setInt(1,talentId);
				resultSet = prepareStatement.executeQuery();
				if(resultSet.next()){
					ResumeEntity resumeEntity = new ResumeEntity();
					resumeEntity.setId(resultSet.getInt("id"));
					resumeEntity.setTalentId(resultSet.getInt("talent_id"));
					resumeEntity.setIntention(resultSet.getString("intention"));
					resumeEntity.setWorkExperience(resultSet.getString("work_experience"));
					resumeEntity.setProjectExperience(resultSet.getString("project_experience"));
					resumeEntityList.add(resumeEntity);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return resumeEntityList;
	}
	
	
	
	
}
